package edu.njust.httpmanager.service.impl;

import edu.njust.httpmanager.common.EmailTemplate;
import edu.njust.httpmanager.service.IEmailService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailServiceImpl implements IEmailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    public void sendEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);

        message.setText(text);

        mailSender.send(message);
    }


    public void sendVerificationEmail(String to, String recipient, String verificationCode,
                                      int expirationMinutes) {

        // 调用方法生成模板
        String verificationEmail = EmailTemplate.generateVerificationEmail(recipient, verificationCode,
                expirationMinutes, "ApiDoc Team");

        sendEmail(to, "验证码邮件，请勿泄漏", verificationEmail);
    }

}
